// 信息来源下拉框指令
define('infoSourceDirective', ['angular','jquery'], function (angular,$) {
    var m = angular.module('directives');
    m.directive('infoSource', [function () {
        return {
            restrict: 'AE',
            templateUrl: 'js/templates/infoSourceSelect.html',
            replace: false,
            scope:{
                dependents:"="
            },
            controller:['$scope','$filter','infoSourceServers',function($scope,$filter,infoSourceServers){
                $scope.data=[];
                $scope.infoSource=[];
                $scope.dependents="";
                $scope.initSelect=function(index,pid){
                    $scope.data.splice(index+1,$scope.data.length-index);
                    $scope.infoSource.splice(index+1,$scope.infoSource.length-index);
                    if(pid!==""){
                        infoSourceServers.getSourceByPid(pid).then(function(data){
                            if(data.length!==0){
                                $scope.data[index+1]=data;
                            }
                        });
                    }else{
                        $scope.infoSource.splice(index,1);
                    }
                };
                $scope.$watch('infoSource',function(n,o){
                    $scope.dependents=n.join(" ");
                },true);
                $scope.$watch('dependents',function(n,o){
                    if(n!==null && typeof(n)!=="undefined"){
                        $scope.infoSource=n.split(" ");
                    }
                });
                infoSourceServers.getSourceByPid(0).then(function(data){
                    $scope.data[0]=data;
                });
            }]
        };
    }]);
});